package Day2025_02_28;


public class LeetCode24 {
    public ListNode swapPairs(ListNode head) {
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        if (head == null || head.next == null) return head;
        ListNode prev = newHead, cur = prev.next, next = cur.next, nnext = next.next;
        while (cur != null && cur.next != null) {
            prev.next = next;
            next.next = cur;
            cur.next = nnext;
            prev = cur;
            cur = cur.next;
            if (cur != null) next = cur.next;
            if (next != null) nnext = next.next;
        }
        return newHead.next;
    }
}
